<template>
  <div class="container">
    <div class="form-box">
      <div class="logo">
        <img src="@/static/images/login/logo.png" alt="" />
      </div>
      <div class="login-form" v-if="isLogin">
        <div>
          <div class="recommend">公共场所不建议自动登录，以防账号丢失</div>
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginFormRules"
          >
            <el-form-item prop="name">
              <el-input
                v-model="loginForm.name"
                placeholder="昵称/邮箱/手机号"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                placeholder="密码"
                type="password"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" style="width: 100%" @click="login"
                >登录</el-button
              >
            </el-form-item>
          </el-form>
          <div class="actions">
            <span @click="toRegister">免费注册</span>
            <span>忘记密码？</span>
          </div>
        </div>
      </div>
      <div class="register-form" v-else>
        <div class="title">用户注册</div>
        <el-form ref="registerFormRef" :model="registerForm">
          <el-form-item>
            <el-input
              placeholder="昵称/邮箱/手机号"
              v-model="registerForm.name"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              placeholder="密码"
              type="password"
              v-model="registerForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              placeholder="确认密码"
              type="password"
              v-model="registerForm.password1"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="registerForm.verification" placeholder="验证码">
              <template #append>{{ verificationCode }}</template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="danger" style="width: 100%">注册</el-button>
          </el-form-item>
        </el-form>
        <div class="statement">
          完成此注册，即表明您统一了我们的<span>《使用条款和隐私策略》</span>
        </div>
        <div class="action">
          <span @click="toLogin">已有账号，去登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, ref } from 'vue';
import { useRouter } from 'vue-router';

export default defineComponent({
  setup() {
    const router = useRouter();
    const isLogin = ref(true);

    const verificationCode = ref('2s4f');
    const state = reactive({
      loginForm: {
        name: '',
        password: '',
      },
      registerForm: {
        name: '',
        password: '',
        password1: '',
        verification: '',
      },
      loginFormRules: {
        name: [
          {
            required: true,
            message: '账号不能为空',
            trigger: 'blur',
          },
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: 'blur',
          },
        ],
      },
    });

    return {
      ...toRefs(state),
      isLogin,
      verificationCode,
      router,
    };
  },
  methods: {
    toRegister() {
      this.isLogin = false;
    },
    toLogin() {
      this.isLogin = true;
    },
    login() {
      if (
        this.loginForm.name === 'admin' &&
        this.loginForm.password === '123456'
      ) {
        this.router.push({
          path: '/index',
        });
      }
    },
  },
});
</script>

<style scoped>
.container {
  width: 100vw;
  height: 100vh;
  background-image: url('~@/static/images/login/bg.jpg');
  background-repeat: no-repeat;
  background-size: cover;
}
.form-box {
  position: absolute;
  top: 30%;
  left: 60%;
  width: 464px;
  background-color: white;
  box-shadow: 0px 0px 10px 0px #ddd;
  padding-bottom: 30px;
}
.form-box .logo {
  width: 100%;
  height: 85px;
  text-align: center;
  border-bottom: 1px solid rgba(225, 225, 225, 0.5);
  margin-bottom: 10px;
}
.form-box .logo img {
  height: 45px;
  margin-top: 20px;
}
/* 用户登录 */
.form-box .login-form {
  width: 256px;
  margin: 0 auto;
}
.form-box .login-form .recommend {
  width: 100%;
  height: 30px;
  font-size: 10px;
  line-height: 30px;
  text-align: center;
  background-color: #fdf4e5;
  border: 1px solid #f7cda4;
  box-sizing: border-box;
  margin-bottom: 10px;
}
.el-input,
.el-button {
  height: 40px;
}
.form-box .login-form .actions {
  font-size: 10px;
  color: #666;
  float: right;
}
.form-box .login-form .actions span {
  display: inline-block;
  margin-left: 10px;
  cursor: pointer;
}
/* 用户注册 */
.form-box .register-form {
  width: 256px;
  margin: 0 auto;
}
.form-box .register-form .title {
  width: 100%;
  height: 30px;
  font-size: 10px;
  text-align: center;
  line-height: 30px;
}
::v-deep .el-input-group__append {
  margin-left: 20px;
  border-left: 1px solid #ccc;
}
.form-box .register-form .statement {
  font-size: 10px;
}
.form-box .register-form .statement span {
  color: red;
}
.form-box .register-form .action {
  font-size: 10px;
  float: right;
}
.form-box .register-form .action span {
  cursor: pointer;
}
</style>
